﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.Drawing.Printing;
using System.Drawing.Imaging;
using System.IO;
using ThoughtWorks.QRCode.Codec;
using ThoughtWorks.QRCode.Codec.Data;
using ThoughtWorks.QRCode.Codec.Util;
using System.Text.RegularExpressions;
using System.Collections;

namespace FormsAppT8
{
    public partial class BQFROMFH: Form
    {

        //dbconn db = new dbconn();

        public BQFROMFH()
        {
            InitializeComponent();
            //comboBox1.SelectedIndex=0;

        }
      
        //清单界面绑定
        public void bdmain()
        {
            bqzk();
            string sql = "";
            string table = "";
            string x1 = textBox1.Text;
            string x2 = textBox12.Text;
            string x3 = textBox8.Text;
            string x4 = textBox7.Text;
            string x5 = textBox10.Text;

            sql = "select distinct top 100 a.billno as 单据编号,a.billdate as 单据日期,a.bizpartnerid as 客户编号 from salDispatchListMaster a,salDispatchListDetail b,comMaterialGroup d where a.billno=b.billno  and b.materialid=d.materialid " +
            " and a.billno like '%" + x1 + "%'" +
            " and a.bizpartnerid  like '%" + x2 + "%'" +
            " and d.materialid  like '%" + x3 + "%'"+
             " and d.materialname  like '%" + x4 + "%'"+
             " and b.materialspec  like '%" + x5 + "%' order by billdate desc";

            table = "salDispatchListMaster";
        
            dataGridView1.Rows.Clear();
            dataGridView1.Columns.Clear();
            dbconn db = new dbconn();
            dataGridView1.Rows.Clear();
            db.bdlm(sql, table, dataGridView1);
            dataGridView2.Rows.Clear();
            dataGridView2.Columns.Clear();
        }

        //明细界面绑定
        public void bddetail()
        {
            bqzk();
            string sql = "";
            string table = "";
            string x1 = textBox1.Text;
            string x2 = textBox12.Text;
            string x3 = textBox8.Text;
            string x4 = textBox7.Text;
            string x5 = textBox10.Text;
            string billno = "";
            if (dataGridView1.SelectedRows.Count > 0)
            {

                billno = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
            }
            sql = "select a.RowNo as 行号,a.MaterialId as 物料代码,b.MaterialName as 物料名称,a.Materialspec as 物料规格,c.CU_DYPMGG as 打印品名规格,"+
" a.BPMaterialId as 客户物料代码,a.BPMaterialName as 客户物料名称,a.BPMaterialSpec as 客户物料规格,cast( a.SQuantity as float) as 本次发货数量,a.SUnitId as 单位,a.frombillno as 销售订单号 "+
" from salDispatchListDetail a join comMaterialGroup b on a.MaterialId=b.MaterialId left join salSalesOrderDetail c on a.FromBillNo=c.BillNo and a.FromRowCode=c.RowCode where  " +
               " a.BillNo='" + billno + "' and b.MaterialId  like '%" + x3 + "%'" + " and b.materialname  like '%" + x4 + "%'" +
             " and a.materialspec  like '%" + x5 + "%'";
            table = "salDispatchListDetail";
                dataGridView2.Rows.Clear();
                dataGridView2.Columns.Clear();
                dbconn db = new dbconn();
                dataGridView2.Rows.Clear();
                db.bdlm(sql, table, dataGridView2);
        }

        //二维码生成
        public void ewmsc()
        {
            //纸箱箱唛二维码带订单号 、物料编码、日期、客户编码、数量
            string data = dataGridView1.SelectedRows[0].Cells[0].Value.ToString() + "|" + dataGridView2.SelectedRows[0].Cells[1].Value.ToString() + "|" + dataGridView2.SelectedRows[0].Cells[10].Value.ToString() + "|" + dataGridView1.SelectedRows[0].Cells[1].Value.ToString() + "|" + dataGridView1.SelectedRows[0].Cells[2].Value.ToString() + "|" + dataGridView2.SelectedRows[0].Cells[8].Value.ToString() + "|";
                //pid + bh + "|" + textBox15.Text + "|" + textBox4.Text + "|" + textBox16.Text + "|" + textBox2.Text + "|" +( Convert.ToDouble(textBox5.Text)*1000).ToString();
            QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
            //设置编码模式 
            qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
            //设置编码测量度
            qrCodeEncoder.QRCodeScale = 2;
            //设置编码版本
            qrCodeEncoder.QRCodeVersion = 4;
            //设置编码错误纠正 
            qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.L;

            Image image;
            //生成二维码图片
            image = qrCodeEncoder.Encode(data, Encoding.UTF8); //Encoding.UTF8);

            pictureBox2.Image = image;
            pictureBox3.Image = image;
        }

        //标签信息显示
        public void bqxx()
        {
            

        }

        //标签内容置空
        public void bqzk()
        {
            label50.Text = "";
            label16.Text = "";
            label7.Text = "";
            //label23.Text = "";
            //label37.Text = "";
        }

        private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
        {
            Pen p = new Pen(System.Drawing.Color.FromArgb(255, System.Drawing.Color.Black), 1); //线条颜色和粗细
            Pen pp = new Pen(System.Drawing.Color.Black, 0.3F);
            Font MyFont1 = new Font("微软雅黑", 9);
            Font MyFont2 = new Font("微软雅黑", 14);

            e.Graphics.PageUnit = GraphicsUnit.Millimeter;   //位置度量单位设为毫米
            e.Graphics.TranslateTransform(0F, 0F);  //偏移量
            ////二维码
            Bitmap _NewBitmap = new Bitmap(200, 200);
            pictureBox2.DrawToBitmap(_NewBitmap, new Rectangle(0, 0, _NewBitmap.Width, _NewBitmap.Height));
            e.Graphics.DrawImage(_NewBitmap, 50, 5, 70, 70);
            //标题
            e.Graphics.DrawString(label36.Text, MyFont2, Brushes.Black, 5, 5);
            //e.Graphics.DrawString(label37.Text, MyFont1, Brushes.Black, 20, 6);
            e.Graphics.DrawString(label48.Text, MyFont1, Brushes.Black, 5, 11);
            e.Graphics.DrawString(label47.Text, MyFont1, Brushes.Black, 5, 17);
            e.Graphics.DrawString(label45.Text, MyFont1, Brushes.Black, 5, 23);
            e.Graphics.DrawString(label44.Text, MyFont1, Brushes.Black, 5, 29);
            e.Graphics.DrawString(label37.Text, MyFont2, Brushes.Black, 36, 34);
            ////画表格横线
            e.Graphics.DrawLine(pp, new Point(5, 41), new Point(75, 41));

            //标签内容
            e.Graphics.DrawString(label6.Text, MyFont1, Brushes.Black, 50, 42);
            e.Graphics.DrawString(label33.Text, MyFont1, Brushes.Black, 5, 42);
            e.Graphics.DrawString(label38.Text, MyFont1, Brushes.Black, 5, 48);
            e.Graphics.DrawString(label50.Text, MyFont1, Brushes.Black, 5, 54);
            e.Graphics.DrawString(label16.Text, MyFont1, Brushes.Black, 5, 60);
            e.Graphics.DrawString(label7.Text, MyFont1, Brushes.Black, 5, 66);
           
        }

        private void printDocument2_PrintPage(object sender, PrintPageEventArgs e)
        {
            Font MyFont2 = new Font("微软雅黑", 16);

            e.Graphics.PageUnit = GraphicsUnit.Millimeter;   //位置度量单位设为毫米
            e.Graphics.TranslateTransform(0F, 0F);  //偏移量

            e.Graphics.DrawString(label18.Text, MyFont2, Brushes.Black, 5, 15);
            //e.Graphics.DrawString(label8.Text, MyFont2, Brushes.Black, 40, 5);
            e.Graphics.DrawString(label17.Text, MyFont2, Brushes.Black, 5, 35);
            e.Graphics.DrawString(label20.Text, MyFont2, Brushes.Black, 5, 50);
            //e.Graphics.DrawString(label13.Text, MyFont2, Brushes.Black, 40, 50);
        }

        private void FormT8BQ_Load(object sender, EventArgs e)
        {

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
          
        }

        private void button1_Click(object sender, EventArgs e)
        {
            bdmain();
            bddetail();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }


        private void textBox18_TextChanged(object sender, EventArgs e)
        {
            //label37.Text = textBox18.Text;
            ewmsc();
        }


        private void textBox9_TextChanged(object sender, EventArgs e)
        {

            //label23.Text = textBox9.Text;
            ewmsc();
        }

     

        private void vistaButton1_Click(object sender, EventArgs e)
        {
            ewmsc();
           // if (textBox9.Text == "") { MessageBox.Show("请输入包装数量！", "提示信息"); }
           // else 
            if (textBox19.Text == "") { MessageBox.Show("请选择打印份数！", "提示信息"); }
            else if (Convert.ToInt16(textBox19.Text) > 0)
            {
                for (int q1 = 0; q1 < Convert.ToInt16(textBox19.Text); q1++)
                {
                    //设置打印时横向还是纵向
                    //printDocument1.PrinterSettings.Copies = Convert.ToInt16(textBox10.Text);
                    printDocument1.DefaultPageSettings.Landscape = false;
                    printDocument1.DefaultPageSettings.Margins = new Margins(20, 20, 20, 0);
                    printDocument1.Print();
                    //printDialog1.Document = printDocument1;
                    //显示打印预览
                    //DialogResult result = printDialog1.ShowDialog();
                    //if (result == DialogResult.OK)
                    //{
                    //    printDocument1.Print();
                    //}
                    //dbconn db = new dbconn();
                    //db.sqlcommand("update capMembership set cu_tmcs='" + (Convert.ToInt16(label46.Text) + 1).ToString() + "' where userid='" + label27.Text + "'");
                    ewmsc();
                }
            }
        }

      


        private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            bddetail();
        }

   
        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            bddetail();
            //第二个标签
            string lxr = "select LinkMan from salDispatchListMaster a where BillNo='" + dataGridView1.SelectedRows[0].Cells[0].Value.ToString() + "'";
            string cs = "select c.ProvinceName,b.CityName from comBusinessPartner a,comCity b,comProvince c where a.ProvinceId=c.ProvinceId and a.CityId=b.CityId and a.BizPartnerId='" + dataGridView1.SelectedRows[0].Cells[2].Value.ToString() + "'";
            label18.Text = "地  区：" + dbconn.data(cs, "comBusinessPartner", "ProvinceName") + dbconn.data(cs, "comBusinessPartner", "CityName");
            //label8.Text = "市：" + dbconn.data(cs, "comBusinessPartner", "CityName");
            label17.Text = "联系人：" + dbconn.data(lxr, "salDispatchListMaster", "LinkMan");
        }

        private void dataGridView2_SelectionChanged(object sender, EventArgs e)
        {
            if (dataGridView2.SelectedRows.Count > 0)
            {
                //第一个标签
                label33.Text = "编码：" + dataGridView2.SelectedRows[0].Cells[1].Value.ToString();
                //label8.Text = "Name：" + dataGridView2.SelectedRows[0].Cells[2].Value.ToString();
                if (dataGridView2.SelectedRows[0].Cells[4].Value == null)
                {
                    label38.Text = "规格：" + dataGridView2.SelectedRows[0].Cells[3].Value.ToString();
                }
                else if (dataGridView2.SelectedRows[0].Cells[4].Value.ToString() == "")
                {
                    label38.Text = "规格：" + dataGridView2.SelectedRows[0].Cells[3].Value.ToString();
                }
                else
                {
                    label38.Text = "规格：" + dataGridView2.SelectedRows[0].Cells[4].Value.ToString();
                }

                label50.Text = "客户物料编码：" + dataGridView2.SelectedRows[0].Cells[5].Value.ToString();
                label16.Text = "客户物料名称：" + dataGridView2.SelectedRows[0].Cells[6].Value.ToString();
                label7.Text = "客户物料规格：" + dataGridView2.SelectedRows[0].Cells[7].Value.ToString();

                //第三个标签
               label23.Text = "Code：" + dataGridView2.SelectedRows[0].Cells[1].Value.ToString();
               //label8.Text = "Name：" + dataGridView2.SelectedRows[0].Cells[2].Value.ToString();
               if (dataGridView2.SelectedRows[0].Cells[4].Value == null)
               {
                   label22.Text = "Spec：" + dataGridView2.SelectedRows[0].Cells[3].Value.ToString();
               }
               else if (dataGridView2.SelectedRows[0].Cells[4].Value.ToString() == "")
               {
                   label22.Text = "Spec：" + dataGridView2.SelectedRows[0].Cells[3].Value.ToString();
               }
               else
               {
                   label22.Text = "Spec：" + dataGridView2.SelectedRows[0].Cells[4].Value.ToString();
               }

               label28.Text = "CustCode：" + dataGridView2.SelectedRows[0].Cells[5].Value.ToString();
               label15.Text = "CustName：" + dataGridView2.SelectedRows[0].Cells[6].Value.ToString();
               label25.Text = "CustSpec：" + dataGridView2.SelectedRows[0].Cells[7].Value.ToString();
               label40.Text = dataGridView2.SelectedRows[0].Cells[10].Value.ToString();
               label41.Text = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
               label43.Text = dataGridView1.SelectedRows[0].Cells[2].Value.ToString();
               label47.Text = dataGridView2.SelectedRows[0].Cells[10].Value.ToString();
               label45.Text = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
               label44.Text = dataGridView1.SelectedRows[0].Cells[2].Value.ToString();
               ewmsc();
               bqxx();
            }

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            label6.Text = "数量：" +textBox2.Text+ " PCS";
            try
            {
                textBox19.Text = (Convert.ToInt16(dataGridView2.SelectedRows[0].Cells[8].Value.ToString()) / Convert.ToInt16(textBox2.Text)).ToString();
            }
            catch { }
            finally { }

        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {
            label20.Text ="总件数："+ "1/" + textBox3.Text;
            //label13.Text = ;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (textBox3.Text == "") { MessageBox.Show("请输入总件数！", "提示信息"); }
            else if (Convert.ToInt16(textBox3.Text) > 0)
            {
                for (int q1 = 0; q1 < Convert.ToInt16(textBox3.Text); q1++)
                {
                    //设置打印时横向还是纵向
                    //printDocument1.PrinterSettings.Copies = Convert.ToInt16(textBox10.Text);
                    label20.Text = "总件数：" + (q1 + 1).ToString() + "/" + textBox3.Text;
                    //label13.Text = (q1+1).ToString()+"/" + textBox3.Text;
                    printDocument2.DefaultPageSettings.Landscape = false;
                    printDocument2.DefaultPageSettings.Margins = new Margins(20, 20, 20, 0);
                    printDocument2.Print();
                }
            }
        }

        private void printDocument3_PrintPage(object sender, PrintPageEventArgs e)
        {
            Pen p = new Pen(System.Drawing.Color.FromArgb(255, System.Drawing.Color.Black), 1); //线条颜色和粗细
            Pen pp = new Pen(System.Drawing.Color.Black, 0.3F);
            Font MyFont1 = new Font("微软雅黑", 9);
            Font MyFont2 = new Font("微软雅黑", 14);

            e.Graphics.PageUnit = GraphicsUnit.Millimeter;   //位置度量单位设为毫米
            e.Graphics.TranslateTransform(0F, 0F);  //偏移量
            ////二维码
            Bitmap _NewBitmap = new Bitmap(200, 200);
            pictureBox3.DrawToBitmap(_NewBitmap, new Rectangle(0, 0, _NewBitmap.Width, _NewBitmap.Height));
            e.Graphics.DrawImage(_NewBitmap, 50, 5, 70, 70);
            //标题
            e.Graphics.DrawString(label19.Text, MyFont2, Brushes.Black, 5, 5);
            //e.Graphics.DrawString(label37.Text, MyFont1, Brushes.Black, 20, 6);
            e.Graphics.DrawString(label39.Text, MyFont1, Brushes.Black, 5, 11);
            e.Graphics.DrawString(label40.Text, MyFont1, Brushes.Black, 5, 17);
            e.Graphics.DrawString(label41.Text, MyFont1, Brushes.Black, 5, 23);
            e.Graphics.DrawString(label43.Text, MyFont1, Brushes.Black, 5, 29);
            e.Graphics.DrawString(label35.Text, MyFont2, Brushes.Black, 36, 34);
            ////画表格横线
            e.Graphics.DrawLine(pp, new Point(5, 41), new Point(75, 41));
           
            //标签内容
            e.Graphics.DrawString(label13.Text, MyFont1, Brushes.Black, 50, 42);
            e.Graphics.DrawString(label23.Text, MyFont1, Brushes.Black, 5, 42);
            e.Graphics.DrawString(label22.Text, MyFont1, Brushes.Black, 5, 48);
            e.Graphics.DrawString(label28.Text, MyFont1, Brushes.Black, 5, 54);
            e.Graphics.DrawString(label15.Text, MyFont1, Brushes.Black, 5, 60);
            e.Graphics.DrawString(label25.Text, MyFont1, Brushes.Black, 5, 66);

            //e.Graphics.DrawString(label44.Text, MyFont1, Brushes.Black, 20, 73);

        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {

            label13.Text = "Qty：" + textBox4.Text + " PCS";
            try
            {
                textBox5.Text = (Convert.ToInt16(dataGridView2.SelectedRows[0].Cells[8].Value.ToString()) / Convert.ToInt16(textBox4.Text)).ToString();
            }
            catch { }
            finally { }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (textBox5.Text == "") { MessageBox.Show("请输入打印份数！", "提示信息"); }
            else if (Convert.ToInt16(textBox5.Text) > 0)
            {
                for (int q1 = 0; q1 < Convert.ToInt16(textBox5.Text); q1++)
                {
                    //设置打印时横向还是纵向
                    //printDocument1.PrinterSettings.Copies = Convert.ToInt16(textBox10.Text);
                    //label20.Text = "总件数：" + (q1 + 1).ToString() + "/" + textBox3.Text;
                    //label13.Text = (q1+1).ToString()+"/" + textBox3.Text;
                    printDocument3.DefaultPageSettings.Landscape = false;
                    printDocument3.DefaultPageSettings.Margins = new Margins(20, 20, 20, 0);
                    printDocument3.Print();
                }
            }
        }

    }
}
